package weaver.jobs.cetc.duss.utils;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2023年08月11日 10:07
 */
public class CetcUtil extends BaseBean {
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        writeLog("-----WorkflowCreateByRequestMap--------");
        writeLog("creatorId:" + creatorId);
        writeLog("workflowId:" + workflowId);
        writeLog("requestName:" + requestName);
        writeLog("IsNextFlow:" + IsNextFlow);

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);
        requestInfo.setWorkflowid(workflowId);
        requestInfo.set_Remark("");
        requestInfo.setDescription(requestName);
        if ("0".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            writeLog("\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue());
            p++;
        }
        writeLog("-----p=" + p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
            try {
                requestid = service.createRequest(requestInfo);
            } catch (Exception e) {
                writeLog("=======" + e.getMessage());
            }
            StringBuffer sbf = new StringBuffer("\r\n-----log begin");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----name:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----time:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----createWf:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----request:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----request:" + requestid);
            sbf.append("\r\n-----create end");
            writeLog(sbf.toString());
            System.out.println("" + sbf.toString());
        } catch (Exception e) {
            writeLog("err��" + e);
            System.out.println("err��" + e);
        }
        return requestid;
    }
    public  Map<String, String> getWorkflowId(String formid) {
        RecordSet rs = new RecordSet();
        Map<String, String> resultMap = new HashMap<>();
        rs.executeQuery("select (case when activeversionid is null then id else activeversionid end) from workflow_base " +
                "where abs(formid)=? group by (case when activeversionid is null then id else activeversionid end) ", formid);
        rs.next();
        String workflowid = Util.null2String(rs.getString(1));
        resultMap.put("workflowid", workflowid);
        rs.executeQuery("select workflowname from workflow_base where id=?", workflowid);
        rs.next();
        String workflowname = Util.null2String(rs.getString(1));
        resultMap.put("workflowname", workflowname);
        return resultMap;
    }

    public  String getUserIdByName(String username) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from hrmresource where lastname=?", username);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     *@Description: 日期格式化返回yyyy-MM-dd
     */
    public  String formatDateToOA(String datestr) {
        String formatTime = "";
        if (!"".equals(datestr) && Util.getIntValue(datestr, -1) > 0) {
            try {
                SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
                Date date1 = date.parse(datestr);
                formatTime = new SimpleDateFormat("yyyy-MM-dd").format(date1);
            } catch (ParseException e) {
                formatTime = datestr;
            }
        }
        return formatTime;
    }


}
